Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
window.onload = function () {
        let url = window.location.href
        let off = url.lastIndexOf('#')
        let tar = url.substr(off)
        if (off == -1) return;
        $("html,body").animate({ scrollTop: $(tar).offset().top-$(window).height()+80 }, 1000);
}

////////////////////////////////////////////////////////////////返回顶部
$(function(){
    $(window).scroll(function(){  //只要窗口滚动,就触发下面代码
        var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //获取滚动后的高度
        if( scrollt >10 ){
            $("#back_top").fadeIn(400); //淡入
        }else{
            $("#back_top").stop().fadeOut(400); //如果返回或者没有超过,就淡出.必须加上stop()停止之前动画,否则会出现闪动
        }
    });
    $("#back_top").click(function(){ //当点击标签的时候,使用animate在200毫秒的时间内,滚到顶部
        $("html,body").animate({scrollTop:"0px"},200);
    });


});
//////////////////////////////////////////////////////////////////推荐显示
// $(function () {
//     jQuery.post("/demo/search", {"userId":-1, "blogId":-1}, function(rst) {
//         //alert(rst.length)
//         let trs = '';
//         var converter = new showdown.Converter(); //初始化转换器
//         for (let i = 0; i < 10; i++) {
//             let u = rst[i];
//             var htmlcontent = converter.makeHtml(u.content); //将MarkDown转为html格式的内容
//             let temp = "/" + "${u.userId}" + "/" + "${u.blogId}";
//             trs += `
//         <div class="first all">
//             <div class="panel-body">
//                 <div class="index-title">
//                     <div class="title text-center text">
//                         <a href="${u.blogId}" class="header-title text" style="font-size: large">${u.blogTitle}</a>
//                     </div>
//                     <hr style="margin: 0 0 10px 0;">
//                 </div>
//                 <hr style="margin: 10px 0 0 0;">
//             </div>
//         </div>
//           `;
//         }
//
//         jQuery("#recommendBody").html(trs);
//     })
// })

/////////////////////////////////////////////////////////////////显示评论
let commentList
function do_show(){
    jQuery.post("/demo/search/comment", {"blogId":$.cookie("blogId")}, function(rst) {
        let trs='';
        commentList = rst
        for (i = 0; i < commentList.length; i ++ ) {
            let u = commentList[i];
            trs += `
            <li class="comment-line-box" id=commentId_${u.commentId} style="list-style-type: none">
                <hr style="margin: 5px 0 0 0;">
                <div class="comment-list-item">
                    <div>
                        <a class="d-flex" href="/demo/home/${u.userId}">
                            <img src="${u.userInfo.userIcon}" style="border-radius: 50%; width:30px; height:30px;">
                            <h5>${u.userInfo.userName}</h5>
                        </a>
                        
                    </div>
                    <div class="comment_body mt-2">${u.content}</div>
                </div>
                <div class="mt-3"></div>
            </li>
            `
        }
        jQuery("#start").html(trs);
    });

}

//////////////////////////////////////////////////// 提交评论
$(function () {   // 提示后刷新
    do_show()
})

jQuery(function() {
    jQuery("#btncomment").click(function() {
        let correntUserId = $.cookie("userId")  // 当前登录用户的Id
        if (!correntUserId) {
            alert("请先登录");
        } else if($("#com-content").text().length <= 0 || $("#com-content").text().length > 50) {
            alert("内容为空或超出限制")
        } else {
            let correntBlogId = $.cookie("blogId")  // 当前blogId
            var commentContent = $('#com-content').val();  // 获取评论内容
            var currtime = new Date().Format("yyyyMMddHHmmss");
            let commentInfo = {
                "createDate":currtime,
                "userId":correntUserId,
                "blogId":correntBlogId,
                "content":commentContent
            }
            //alert(commentContent);
            jQuery.post("/demo/comment/submit", commentInfo, function(rst) {
                //alert(rst);
                $("#com-content").val("");
                do_show();
            });
        }



    });
});

//////////////////////////////////////////////////////////////////// 博客详情展示页显示博客
jQuery(function() {
    var correntBlogId = $.cookie("blogId")
    //alert(correntBlogId)
    jQuery.post("/demo/blog/"+correntBlogId, function(blogInfo) {
        // 将blog显示出来
        //alert(blogInfo.content)
        var converter = new showdown.Converter({tables:true}); //初始化转换器
        var htmlcontent = converter.makeHtml(blogInfo.content); //将MarkDown转为html格式的内容
        //alert(htmlcontent)
        let time = blogInfo.createDate
        let pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        var createTime = time.replace(pattern, '$1/$2/$3 $4:$5:$6');
        let Info = blogInfo
        let trs = '';
        let from = blogInfo.blogFromOther
        let path = ""
        var addin = ""
        if (from == "false") {
            addin = "作者:" + Info.userInfo.userName
            path = "/demo/home/" + Info.userInfo.userId + ""
        }
        else {
            addin = "转载自:" + from
            path = from
        }

        let tt = '文章标签:'
        for (let i = 0; i < blogInfo.blogTags.length; i ++ ) {
            tt += "<span class='badge badge-pill badge-success' style='background-color: #d7d4f0'><a href='"
            tt += "/demo/tags/"
            tt += blogInfo.blogTagId[i].toString()
            tt += "'"
            tt += ">" + blogInfo.blogTags[i] + "</a></span>"
        }

        trs += `
        
        <h2 align="center" style="background-color: #d7d4f0" class="m-2">${Info.blogTitle}</h2>
        <a href=${path} class="col-lg-12">
            ${addin}
        </a>
        <div class="col-8">
            创作时间：${createTime}
        </div>
        <div class="col-lg-12">
            ${tt}
        </div>
        <hr style="margin: 10px 0 0 0;">
        <div class="m-3">
            <div>${htmlcontent}</div>
        </div>
        `
        //alert(trs)
        jQuery("#blogcontent").html(trs);
        hljs.initHighlightingOnLoad();         // 代码段高亮检测
    })
})




